package com.marvels.approve.process.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.marvels.approve.common.utils.ApplicationContextAchieve;
import com.marvels.approve.loan.dao.IMcamLoanApplyInfoDao;
import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.manager.dao.IOpinionDao;
import com.marvels.approve.manager.dto.OpinionDTO;
import com.marvels.approve.process.service.NodeFlowService;
import com.marvels.common.constants.CaseNodeConstant;
import com.marvels.common.utils.ServerProperties;
@Service
public class NodeFlowServiceImpl implements NodeFlowService {
	@Value("${config.properties.path}")
	private String configPropertiesPath;
	@Autowired
	private IMcamLoanApplyInfoDao mcamLoanApplyInfoDao;
	@Autowired
	private IOpinionDao opinionDao;
	
	public static void main(String[] args) {
		System.out.println(new Random().nextInt(100));
	}
	@Override
	public boolean selectViolateCode(String loanId) {
		if(StringUtils.isEmpty(loanId)){
			return false;
		}
		int randomRate = new Random().nextInt(100);//随机数
		int random = Integer.parseInt(ServerProperties.getValue(configPropertiesPath, "resource.properties", "user.randomRate"));//配置机率数
		if(randomRate <= random){//随机数小于配置机率率，强制走审计
			return true;
		}
		
		McamLoanApplyInfoDTO mcamLoanApplyInfoDTO = mcamLoanApplyInfoDao.selectMcamLoanApplyInfoByLoanId(loanId);
		if(null==mcamLoanApplyInfoDTO){
			return false;
		}
		String rateTwoAct = ServerProperties.getValue(configPropertiesPath, "resource.properties", "user.principalAmtAct");
		double paramValue = Double.valueOf(rateTwoAct).doubleValue();//配置最大额度
		double principalAmtAct = mcamLoanApplyInfoDTO.getPrincipalAmtAct().doubleValue();//实际贷款额度
		if (principalAmtAct > paramValue)  {//实际贷款额度大于配置额度，强制走审计
			return true;
		}
		String channel = null;
		Map map = new HashMap();
		map.put("opinionClass", "1");
		map.put("nodeCode", CaseNodeConstant.WOAPPROVAL.getCode());
		map.put("loanId", loanId);
		OpinionDTO opinion = new OpinionDTO();
		String opinionList = opinionDao.selectOpinionOne(map);
		String viloate = null;
		if (!StringUtils.isEmpty(opinionList)) {
			if ("101".equals(mcamLoanApplyInfoDTO.getChannel())) {
				channel = ServerProperties.getValue(configPropertiesPath, "resource.properties", "user.violateCde.101");
				if(channel.indexOf(opinionList) != -1){//违例代码 在配置违例代码中，强制走审计
					return true;
				}
			}else if ("102".equals(mcamLoanApplyInfoDTO.getChannel())) {
				channel = ServerProperties.getValue(configPropertiesPath, "resource.properties", "user.violateCde.102");
				if(channel.indexOf(opinionList) != -1){//违例代码 在配置违例代码中，强制走审计
					return true;
				}
			}
		}
		
		return false;
	}

}
